package edu.colorado.karl.devices;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.colorado.karl.databases.ErrorDatabaseResult;
import edu.colorado.karl.databases.LongTermDatabase;
import edu.colorado.karl.databases.SensorResponse;

/**
 * Represents the Water Tanks device.  This device is responsible for obtaining
 * and displaying the following information:
 * <ul><li>Hot Tank Temperature Sensor
 * <li>Cold Tank Temperature Sensor
 * <li>Hot Tank and Cold Tank Set Points
 * <li>In/Out Valve Status (not implemented)
 * <li>If the Tank Pumps are active and why (not implemented)
 * </ul>
 * @author Michael Knudson
 * @version 4/27/08
 */
public class WaterTanks {
	//	Used for log4j logging
	private static final Log log = LogFactory.getLog(WaterTanks.class);

	/**
	 * Sole constructor.
	 */
	public WaterTanks() {
		
	}
	
	/**
	 * Returns the last hot and cold tank temperatures recorded in the 
	 * Long-Term Database.  If an error occurs while retrieving a temperature,
	 * the Long_Term_Fail error is set and the value "0.0" is returned for
	 * that temperature.
	 * @return a two-element double array containing the air temperatures.
	 * Index 0 contains the hot tank temperature while index 1 contains the
	 * cold tank temperature.
	 */
	public double[] getTankTemperatures() {
		LongTermDatabase ltdb = new LongTermDatabase();
		double[] temps = new double[2];
		temps[0] = -666;
		temps[1] = -666;
		
		//	Get tank temperatures from the LongTerm database
		ArrayList<SensorResponse> ltl = ltdb.getTankTemperatures();

		for (SensorResponse response : ltl) {
			if(response.getName().equals("HT"))
				temps[0] = Double.parseDouble(response.getValueString());
			else if(response.getName().equals("CT"))
				temps[1] = Double.parseDouble(response.getValueString());
		}

		if(temps[0] == -666) {
			log.error("Current State unable to find current Hot Tank Temperature.");
			temps[0] = 0.0;
			ErrorDatabaseResult.activateError(ErrorDatabaseResult.LONG_TERM_FAIL);
		}
		if(temps[1] == -666) {
			log.error("Current State unable to find current Cold Tank Temperature.");
			temps[1] = 0.0;
			ErrorDatabaseResult.activateError(ErrorDatabaseResult.LONG_TERM_FAIL);
		}
		
		return temps;
	}
}
